@using BlazorApp.Pages.Common
@using BlazorApp.Pages.Common.Html
@using BlazorApp.Pages.Common.Html.HTable
@using BlazorApp.Pages.Common.Metadata
@using k8s
@using k8s.Models
@inherits BlazorApp.Pages.Common.PageBase

@if (Containers != null)
{
    @foreach (var c in Containers)
    {
        <BadgeRibbon Text="@c.Name" Placement="start">
            <Card Bordered>
                <HTable Title="" Bordered Column="1">
                    <HTableColumn Title=@L["Name"] ExplainField="pod.spec.containers.name">
                        <ContainerType Type="@Type"></ContainerType>
                        @c.Name
                    </HTableColumn>
                    <PropertySimpleView Title=@L["Image"] Item="@c.Image" ExplainField="pod.spec.containers.name"></PropertySimpleView>
                    <PropertyOptionView Title=@L["ImagePullPolicy"] ExplainField="pod.spec.containers.imagePullPolicy" Item="@c.ImagePullPolicy" Options='@(new[] { "Always", "IfNotPresent", "Never" })'></PropertyOptionView>
                    <ContainerResourceRequestLimitsView RequestLimits="@c.Resources" ExplainField="pod.spec.containers.resources"></ContainerResourceRequestLimitsView>
                    <PropertySimpleView Title=@L["EnvFrom"] Item="@c.EnvFrom" ExplainField="pod.spec.containers.envFrom" ShowInJson="true"></PropertySimpleView>
                    <PropertySimpleView Title=@L["Lifecycle"] Item="@c.Lifecycle" ExplainField="pod.spec.containers.lifecycle" ShowInJson="true"></PropertySimpleView>
                    <PropertySimpleView Title=@L["ResizePolicy"] Item="@c.ResizePolicy" ExplainField="pod.spec.containers.resizePolicy" ShowInJson="true"></PropertySimpleView>
                    <PropertyOptionView Title=@L["TerminationMessagePolicy"] Item="@c.TerminationMessagePolicy" ExplainField="pod.spec.containers.terminationMessagePolicy" Options='@(new[] { "FallbackToLogsOnError", "File" })'></PropertyOptionView>
                    <PropertySimpleView Title=@L["TerminationMessagePath"] Item="@c.TerminationMessagePath" ExplainField="pod.spec.containers.terminationMessagePath"></PropertySimpleView>
                    <PropertySimpleView Title=@L["WorkingDir"] Item="@c.WorkingDir" ExplainField="pod.spec.containers.workingDir"></PropertySimpleView>
                    <PropertySimpleView Title=@L["TTY"] Item="@c.Tty" ExplainField="pod.spec.containers.tty"></PropertySimpleView>
                    <PropertySimpleView Title=@L["Stdin"] Item="@c.Stdin" ExplainField="pod.spec.containers.stdin"></PropertySimpleView>
                    <PropertySimpleView Title=@L["StdinOnce"] Item="@c.StdinOnce" ExplainField="pod.spec.containers.stdinOnce"></PropertySimpleView>
                    <EnvView Env="@c.Env" ExplainField="pod.spec.containers.env"></EnvView>
                    @if (c.Ports != null)
                    {
                        <HTableColumn Title=@L["Ports"] ExplainField="pod.spec.containers.ports">

                            @foreach (var p in c.Ports)
                            {
                                <div>
                                    @($"{p.Name}:{p.ContainerPort}/{p.Protocol}")
                                    @if (ShowPortForwardBtn)
                                    {
                                        @if (!KubernetesClientConfiguration.IsInCluster())
                                        {
                                            <Button Type="@ButtonType.Primary" OnClick="@(() => OnPortForwardBtnClick(p.ContainerPort))">Forward</Button>
                                        }
                                    }
                                </div>
                            }
                        </HTableColumn>
                    }
                    @if (c.Command != null)
                    {
                        <HTableColumn Title=@L["Command"] ExplainField="pod.spec.containers.command">
                            @foreach (var cc in c.Command)
                            {
                                @cc
                                <br/>
                            }
                        </HTableColumn>
                    }
                    @if (c.Args != null)
                    {
                        <HTableColumn Title=@L["Args"] ExplainField="pod.spec.containers.args">
                            @foreach (var cc in c.Args)
                            {
                                @cc
                                <br/>
                            }
                        </HTableColumn>
                    }
                    <ProbeView ExplainField="pod.spec.containers.livenessProbe" Probe="@c.LivenessProbe"
                               ProbeType="LivenessProbe">
                    </ProbeView>
                    <ProbeView ExplainField="pod.spec.containers.readinessProbe" Probe="@c.ReadinessProbe"
                               ProbeType="ReadinessProbe">
                    </ProbeView>
                    <ProbeView ExplainField="pod.spec.containers.startupProbe" Probe="@c.StartupProbe"
                               ProbeType="StartupProbe">
                    </ProbeView>

                    @if (c.VolumeMounts != null)
                    {
                        <HTableColumn Title=@L["VolumeMounts"] ExplainField="pod.spec.containers.volumeMounts">
                            @foreach (var vm in c.VolumeMounts)
                            {
                                <div>
                                    @if (vm.ReadOnlyProperty is true)
                                    {
                                        <ColorfulTag>RO</ColorfulTag>
                                    }
                                    else
                                    {
                                        <ColorfulTag>RW</ColorfulTag>
                                    }

                                    <ColorfulTag>@vm.Name</ColorfulTag>
                                    <span>=></span>
                                    <ColorfulTag>@vm.MountPath</ColorfulTag>
                                </div>
                            }
                        </HTableColumn>
                    }
                </HTable>
                <ContainerSecurityContextView SecurityContext="@c.SecurityContext"></ContainerSecurityContextView>
            </Card>
        </BadgeRibbon>
    }
}

@code {


    [Inject] IMessageService MessageService { get; set; }

    [Parameter] public IList<V1Container> Containers { get; set; }

    [Parameter] public string Type { get; set; }

    [Parameter] public bool ShowPortForwardBtn { get; set; }

    [Parameter] public EventCallback<int> OnPortForward { get; set; }

    private async Task OnPortForwardBtnClick(int port)
    {
        await OnPortForward.InvokeAsync(port);
    }

}
